using System;
using System.Collections;
using System.ComponentModel;
using System.ComponentModel.Design.Serialization;
using System.ComponentModel.Design;
using Widgetsphere.Generator.Models;
using System.Collections.Generic;

namespace Widgetsphere.Generator.Design.Editors
{
	internal class ParentTableSelectEditor : System.Drawing.Design.UITypeEditor
	{
		public ParentTableSelectEditor()
		{
		}

		public override System.Drawing.Design.UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
		{
			return System.Drawing.Design.UITypeEditorEditStyle.DropDown;
		}

		System.Windows.Forms.Design.IWindowsFormsEditorService edSvc = null;
		public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)
		{			
			try
			{
				edSvc = (System.Windows.Forms.Design.IWindowsFormsEditorService)provider.GetService(typeof(System.Windows.Forms.Design.IWindowsFormsEditorService));

				string selectedItemKey = "";
				ModelRoot root = (ModelRoot)((Table)context.Instance).Root;
				Table sourceTable = (Table)context.Instance;
				Table retval = sourceTable.ParentTable;
				selectedItemKey = sourceTable.ParentTableKey;

				List<Table> tableCollection = new List<Table>();
				foreach (Table t in root.Database.Tables)
				{
					tableCollection.Add(t);
				}

				//Remove the current table
				tableCollection.Remove(sourceTable);

				//Remove all parent tables
				List<Table> tList = sourceTable.GetTableHierarchy();
				foreach (Table t in tList)
				{
					if (tableCollection.Contains(t))
						tableCollection.Remove(t);
				}

				//Remove all child tables
				tList = sourceTable.GetTablesInheritedFromHierarchy();
				foreach (Table t in tList)
				{
					if (tableCollection.Contains(t))
						tableCollection.Remove(t);
				}

				//Add the current parent if one exists
				if (sourceTable.ParentTable != null)
					tableCollection.Add(sourceTable.ParentTable);

				//Create the list box 
				System.Windows.Forms.ListBox newBox = new System.Windows.Forms.ListBox();
				newBox.Click += new EventHandler(newBox_Click);
				newBox.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;

				SortedList sortedList = new SortedList();
				foreach (Table table in tableCollection)
				{
					//Ensure key is unique to avoid error
					string text = table.Name.ToLower();
					string key = text;
					int ii = 0;
					while (sortedList.ContainsKey(key))
					{
						key = text + ii.ToString();
						ii++;
					}
					sortedList.Add(key, table);
				}

				//Re-add them in order
				newBox.Items.Add("(None)");
				newBox.SelectedIndex = 0;
				foreach (DictionaryEntry di in sortedList)
				{
					Table t = (Table)di.Value;
					newBox.Items.Add(t);
					if (t.Key == selectedItemKey)
						newBox.SelectedIndex = newBox.Items.Count - 1;
				}

				edSvc.DropDownControl(newBox);
				if ((newBox.SelectedIndex > 0) && (newBox.SelectedItem != null))
					retval = ((Table)newBox.SelectedItem);
				else if (newBox.SelectedIndex == 0)
					retval = null;

				return retval;
			}
			catch (Exception ex) { }
			return null;
		}

		private void newBox_Click(object sender, System.EventArgs e) 
		{ 
			if (edSvc != null)
				edSvc.CloseDropDown();
		}

	}
}
